##parameters=form=None
# from csv import DictWriter
# from StringIO import StringIO

from Products.KnowledgeEngine.Utils.helpers import renderCSV


if not form: form = context.REQUEST.form

results = context.demo_search(form=form)
schema = context.demo_search_mapping()
fields = [field['idx'] for field in schema]

setHeader = context.REQUEST.RESPONSE.setHeader
setHeader( 'content-type', 'text/csv' )
setHeader( 'content-disposition', 'attachment;filename=search_results.csv' )

return renderCSV( fields, results, renderHeader=True )
